//
//  ForgetViewController.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/8.
//

#import "ForgetViewController.h"

@interface ForgetViewController ()
@property (nonatomic, assign) NSInteger timeout;
@property (nonatomic, strong) NSTimer * timer;
@end

@implementation ForgetViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutCustomNav:self.view title:@"忘记密码"];
    
    [self.loginImage createBordersWithColor:nil withCornerRadius:5.0 andWidth:0];
     self.phoneTF.delegate=self;
     self.codeTF.delegate=self;
    [self.searchButton createBordersWithColor:nil withCornerRadius:5.0 andWidth:0];
    [self.codeButton whenTapped:^{
        [self.view endEditing:YES];
        [self getCodeData];
    }];
    [self.searchButton whenTapped:^{
        [self.view endEditing:YES];
        [self commitPassWord];
    }];
    [self.view whenTapped:^{
        [self.view endEditing:YES];
    }];
}

-(void)getCodeData{
   if (![_phoneTF.text isValidPhone]){
        showAlert(@"手机号码不正确");
        return;
    }
    
    self.codeButton.enabled = NO;
    
    NSString *url = [NSString stringWithFormat:@"%@?phone=%@",BEFORE_SMS_URL,self.phoneTF.text];
    
    [MSNetworkManager postRevertUrl:url params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            self.timeout = kCodeTime;
           [self.codeButton setTitle:[NSString stringWithFormat:@"%d秒", kCodeTime] forState:UIControlStateNormal];
            showAlert(@"获取验证码成功");
           [self applicationEnterBackground];
            BD_WEAKSELF
           [[kNotifyCenter rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil] subscribeNext:^(NSNotification * _Nullable note){
               [weakSelf applicationEnterBackground];
           }];
        }
        else {
            [self msgCodeFail];
        }
        
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        showAlert(@"验证码获取失败");
        [self msgCodeFail];
    } withShowHud:YES];
    
 
}

- (void)applicationEnterBackground {
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
    //加入定时器，用来控制后台运行时间
    if (!_timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
        //[_timer fire];
    }
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)countDown {
    _timeout--;
    [self.codeButton setTitle:[NSString stringWithFormat:@"%ld秒",(long)_timeout] forState:UIControlStateNormal];
    if (_timeout == 0){
        [self msgCodeFail];
    }
}

- (void)msgCodeFail {
    [_timer invalidate];
     _timer = nil;
    [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
     _codeButton.enabled = YES;
     _timeout = kCodeTime;
}

-(void)commitPassWord{
    if (![self.phoneTF.text isValidPhone]) {
        showAlert(@"手机号码不正确");
        return;
    }
    if (self.codeTF.text.length == 0){
        showAlert(@"验证码不能为空");
        return;
    }
    if (self.passTF.text.length == 0 || self.passTF.text.length < 8){
        showAlert(@"密码不能为空或长度不能小于8位");
        return;
    }
    if (self.passTF.text.length > 18) {
        showAlert(@"密码长度不能大于18位");
        return;
    }
    if ([self.passTF.text isValidPwd] == NO) {
        showAlert(@"密码必须为数字英文混合设置");
        return;
    }
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    [params setObject:self.phoneTF.text forKey:@"phone"];
    [params setObject:self.passTF.text forKey:@"passWord"];
    [params setObject:self.codeTF.text forKey:@"code"];
    
    
    [MSNetworkManager postRevertUrl:Post_ForgetPassword_Url params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            showAlert(@"密码修改成功,请重新进行登录");
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    

}

-(void)textFieldDidChangeSelection:(UITextField *)textField{
    if (textField == self.phoneTF) {
            if (textField.text.length > 11) {
                textField.text = [textField.text substringToIndex:11];
            }
        }
    if (textField == self.codeTF) {
        if (textField.text.length > 6){
            textField.text = [textField.text substringToIndex:6];
        }
    }
  
}

@end
